package com.imedway.ihe.dtz.xds.client;

import org.junit.jupiter.api.Test;
import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.QueryReturnType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.ResponseOptionType;
import org.openehealth.ipf.commons.ihe.xds.iti18.Iti18PortType;
import org.openehealth.ipf.platform.camel.ihe.xds.core.converters.EbXML30Converters;
import org.springframework.boot.test.context.SpringBootTest;

import com.imedway.ihe.dd.properties.ItiAddressProps;
import com.imedway.ihe.dd.util.QueryRegistryUtil;
import com.imedway.ihe.dd.util.WebserviceUtil;
import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dd.vo.GetAllVO;
import com.imedway.ihe.dd.vo.GetAssociationsVO;
import com.imedway.ihe.dd.vo.GetDocumentsAndAssociationsVO;
import com.imedway.ihe.dd.vo.GetFolderAndContentsVO;
import com.imedway.ihe.dd.vo.GetSubmissionSetsVO;
import com.imedway.ihe.dd.vo.Iti18QueryVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootTest
public class Iti18Client {
	  
	static String serviceWsdl ="http://localhost:8080/services/iti18?wsdl";
	static String serviceNamespaceUri ="http://service.ihe.imedway.com/";
	static String serviceLocalPart = "Iti18PortTypeImplService";
	
	public Iti18PortType getClient() {
		ItiAddressProps itiAddressProps=new ItiAddressProps();
		itiAddressProps.setServiceLocalPart(serviceLocalPart);
		itiAddressProps.setServiceNamespaceUri(serviceNamespaceUri);
		Iti18PortType itiPortType = WebserviceUtil.buildWebService(Iti18PortType.class, serviceWsdl,itiAddressProps);
		return itiPortType;		
	};
	@Test
	public void FIND_DOCUMENTS() {
		FindDocumentsVO documentsQuery = new FindDocumentsVO();
		documentsQuery.set$XDSDocumentEntryPatientId("SELF-5^^^&amp;1.3.6.1.4.1.21367.2005.3.7&amp;ISO");
		Iti18QueryVO<FindDocumentsVO> iti18QueryVO =new Iti18QueryVO<FindDocumentsVO>();		
		iti18QueryVO.setParams(documentsQuery);
		iti18QueryVO.setReturnType(QueryReturnType.OBJECT_REF.getCode());
        // 构建请求参数
        QueryRegistry findDocumentsQuery = QueryRegistryUtil.buildFindDocumentsQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, findDocumentsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void FindSubmissionSets() {
		FindSubmissionSetsVO findSubmissionSetsVO =new FindSubmissionSetsVO();
		findSubmissionSetsVO.set$XDSSubmissionSetPatientId("SELF-5^^^&amp;1.3.6.1.4.1.21367.2005.3.7&amp;ISO");
		Iti18QueryVO<FindSubmissionSetsVO> iti18QueryVO = new Iti18QueryVO<FindSubmissionSetsVO>();
		iti18QueryVO.setParams(findSubmissionSetsVO);
		iti18QueryVO.setReturnType(QueryReturnType.OBJECT_REF.getCode());
        // 构建请求参数
        QueryRegistry findSubmissionSetsQuery = QueryRegistryUtil.buildFindSubmissionSetsQueryRegistry(iti18QueryVO);
        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, findSubmissionSetsQuery);
        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
        log.info(queryResponse.getStatus());
	}
	@Test
	public void FIND_FOLDERS() {
		FindFoldersVO documentsQuery = new FindFoldersVO();
		
		Iti18QueryVO<FindFoldersVO> iti18QueryVO =new Iti18QueryVO<FindFoldersVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry findFoldersQuery = QueryRegistryUtil.buildFindFoldersQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, findFoldersQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void GET_ASSOCIATIONS() {
		GetAssociationsVO documentsQuery = new GetAssociationsVO();
		
		Iti18QueryVO<GetAssociationsVO> iti18QueryVO =new Iti18QueryVO<GetAssociationsVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry getAssociationsQuery = QueryRegistryUtil.buildGetAssociationsQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, getAssociationsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void GET_DOCUMENTS_AND_ASSOCIATIONS() {
		GetDocumentsAndAssociationsVO documentsQuery = new GetDocumentsAndAssociationsVO();
		
		Iti18QueryVO<GetDocumentsAndAssociationsVO> iti18QueryVO =new Iti18QueryVO<GetDocumentsAndAssociationsVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry getDocumentsAndAssociationsQuery = QueryRegistryUtil.buildGetDocumentsAndAssociationsQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, getDocumentsAndAssociationsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void GET_SUBMISSION_SETS() {
		GetSubmissionSetsVO documentsQuery = new GetSubmissionSetsVO();
		
		Iti18QueryVO<GetSubmissionSetsVO> iti18QueryVO =new Iti18QueryVO<GetSubmissionSetsVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry getSubmissionSetsQuery = QueryRegistryUtil.buildGetSubmissionSetsQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, getSubmissionSetsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void GET_FOLDER_AND_CONTENTS() {
		GetFolderAndContentsVO documentsQuery = new GetFolderAndContentsVO();
		
		Iti18QueryVO<GetFolderAndContentsVO> iti18QueryVO =new Iti18QueryVO<GetFolderAndContentsVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry getFolderAndContentsQuery = QueryRegistryUtil.buildGetFolderAndContentsQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, getFolderAndContentsQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
	@Test
	public void GET_ALL() {
		GetAllVO documentsQuery = new GetAllVO();
		
		Iti18QueryVO<GetAllVO> iti18QueryVO =new Iti18QueryVO<GetAllVO>();		
		iti18QueryVO.setParams(documentsQuery);
        // 构建请求参数
        QueryRegistry getAllQuery = QueryRegistryUtil.buildGetAllQueryRegistry(iti18QueryVO);

        // 转换请求参数
        AdhocQueryRequest request = getAdhocQueryRequest(iti18QueryVO, getAllQuery);

        // 请求查询
        AdhocQueryResponse queryResponse = getClient().documentRegistryRegistryStoredQuery(request);
	}
    private static <T> AdhocQueryRequest getAdhocQueryRequest(Iti18QueryVO<T> iti18QueryVO, QueryRegistry queryRegistry) {
        AdhocQueryRequest request = EbXML30Converters.convert(queryRegistry);
        request.setId(iti18QueryVO.getQueryId());
        request.setComment(iti18QueryVO.getRegistry());
        ResponseOptionType optionType = new ResponseOptionType();
        optionType.setReturnType(iti18QueryVO.getReturnType());
        request.setResponseOption(optionType);
        return request;
    }

}
